<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />

    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{ artObj.title }}</h1>

      <!-- 用户信息 -->
      <van-cell
        center
        :title="artObj.aut_name"
        :label="$timeTo(artObj.pubdate)"
      >
        <template #icon>
          <img :src="artObj.aut_photo" alt="" class="avatar" />
        </template>
        <template #default>
          <div>
            <van-button
              type="info"
              size="mini"
              v-if="artObj.is_followed"
              @click="followedFn(false)"
              >已关注</van-button
            >
            <van-button
              icon="plus"
              type="info"
              size="mini"
              plain
              v-else
              @click="followedFn(true)"
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="artObj.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button
          icon="good-job"
          type="danger"
          size="small"
          v-if="artObj.attitude && artObj.attitude !== -1"
          @click="attitudeFn(false)"
          >已点赞</van-button
        >
        <van-button
          icon="good-job-o"
          type="danger"
          plain
          size="small"
          v-else
          @click="attitudeFn(true)"
          >点赞</van-button
        >
      </div>
    </div>
    <!-- 评论区域 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
    <Comment :list="commentList"></Comment>
    </van-list>

    <!-- 底部添加评论区域 - 1 -->
    <div :class="isShowCmtInput ? 'art-cmt-container-1' : 'art-cmt-container-2'">
      <div class="add-cmt-box van-hairline--top" v-if="isShowCmtInput">
        <van-icon name="arrow-left" size="18" @click="$router.back()" />
        <div class="ipt-cmt-div" @click="isShowCmtInput = false">发表评论</div>
        <div class="icon-box">
          <van-badge :content="totalcount" max="99">
            <!-- 点击回到评论区域小标志 -->
            <van-icon
              name="comment-o"
              size="0.53333333rem"
              @click="lookComFn"
            />
          </van-badge>
          <van-icon name="star-o" size="0.53333333rem" />
          <van-icon name="share-o" size="0.53333333rem" />
        </div>
      </div>

      <!-- 底部添加评论区域 - 2 -->
      <div class="cmt-box van-hairline--top" v-else>
        <textarea
          placeholder="友善评论、理性发言、阳光心灵"
          @blur="blurFn"
          v-focus
          v-model.trim="comList"
        ></textarea>
        <van-button type="default" :disabled="comList.length === 0" @click="releaseFn" >发布</van-button>
      </div>
    </div>
  </div>
</template>

<script>
import Comment from './Comment'
import {
  articlesApi,
  followingsApi,
  unFollowingsApi,
  likingsApi,
  unLikingsApi,
  commentsApi,
  releaseCommentsApi
  // releaseCommentsApi
} from '@/api'
export default {
  data () {
    return {
      loading: false, // 控制加载状态
      finished: false, // 控制没有数据之后显示文字
      artObj: {}, // 文章详情对象
      commentList: [], // 评论数据
      isShowCmtInput: true, // 底部发布评论组件显示/隐藏
      comList: '', // 评论文本
      totalcount: 0, // 评论总数量
      lastId: null // 上一次请求的id
    }
  },
  async created () {
    const [, res] = await articlesApi(this.$route.params.art_id)
    // console.log(res)
    this.artObj = res.data.data
  },
  methods: {
    blurFn () {
      setTimeout(() => {
        this.isShowCmtInput = true
      })
    },
    // 滚动动画
    lookComFn () {
      // 获取当前已经滚动出去的距离
      const now = window.scrollY
      // 获取文章的高度 - 就是你要滚出去的高度
      let dist = document.querySelector('.article-container').offsetHeight
      if (dist > document.documentElement.scrollHeight - window.innerHeight) {
        dist = document.documentElement.scrollHeight - window.innerHeight
      }
      // 定义步长/一次滚多少
      const step = (dist - now) / 10
      // 用定时器来操作他往上滚的动画
      setTimeout(() => {
        if (step < 1 && step > 0) {
          window.scrollTo(0, dist)
          return
        }
        window.scrollTo(0, now + step)
        this.lookComFn()
      }, 10)
    },
    // 关注
    async followedFn (bool) {
      this.artObj.is_followed = bool
      if (bool === true) {
        const [err] = await followingsApi({
          target: this.artObj.aut_id
        })
        if (!err) this.$Toast({ icon: 'success', message: '关注成功' })
      } else {
        const [err] = await unFollowingsApi(this.artObj.aut_id)
        if (!err) this.$Toast({ icon: 'success', message: '取消关注' })
      }
    },
    // 点赞
    async attitudeFn (bool) {
      this.artObj.attitude = bool
      if (bool === true) {
        // 要点赞
        const [err] = await likingsApi({ target: this.artObj.art_id })
        if (!err) this.$Toast({ icon: 'success', message: '点赞成功' })
      } else {
        // 取消点赞
        const [err] = await unLikingsApi(this.artObj.art_id)
        if (!err) this.$Toast({ icon: 'success', message: '取消点赞' })
      }
    },
    // 获取评论
    async getCommentList () {
      const [, res] = await commentsApi({
        type: 'a',
        source: this.$route.params.art_id,
        offset: this.lastId
      })
      // console.log(res)
      // console.log(this.artObj)
      if (res?.data?.data?.results.length === 0) {
        // 没有更多数据
        this.finished = true
      }
      this.commentList = [...this.commentList, ...res.data.data.results]
      this.totalcount = res.data.data.total_count
      this.loading = false
      this.lastId = res.data.data.last_id
    },
    // 发布评论
    async releaseFn () {
      // console.log(this.comList)
      const [, res] = await releaseCommentsApi({
        target: this.$route.params.art_id,
        content: this.comList
      })
      console.log(res)
      this.commentList.unshift(res.data.data.new_obj)
      this.comList = ''
      this.totalcount++
    },
    onLoad () {
      // 触底加载更多
      // 请求: 评论列表数据
      this.getCommentList()
    }
  },
  components: {
    Comment
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
/*美化 - 文章详情 - 底部的评论页面 */
// 外层容器
.art-cmt-container-1 {
  padding-bottom: 46px;
}
.art-cmt-container-2 {
  padding-bottom: 80px;
}

// 发布评论的盒子 - 1
.add-cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  box-sizing: border-box;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 46px;
  line-height: 46px;
  padding-left: 10px;
  .ipt-cmt-div {
    flex: 1;
    border: 1px solid #efefef;
    border-radius: 15px;
    height: 30px;
    font-size: 12px;
    line-height: 30px;
    padding-left: 15px;
    margin-left: 10px;
    background-color: #f8f8f8;
  }
  .icon-box {
    width: 40%;
    display: flex;
    justify-content: space-evenly;
    line-height: 0;
  }
}

.child {
  width: 20px;
  height: 20px;
  background: #f2f3f5;
}

// 发布评论的盒子 - 2
.cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  padding-left: 10px;
  box-sizing: border-box;
  background-color: white;
  textarea {
    flex: 1;
    height: 66%;
    border: 1px solid #efefef;
    background-color: #f8f8f8;
    resize: none;
    border-radius: 6px;
    padding: 5px;
  }
  .van-button {
    height: 100%;
    border: none;
  }
}
</style>
